package com.wolfcode.contract.controller;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wolfcode.contract.domain.info.ContractInfo;
import com.wolfcode.contract.domain.vo.ContractVo;
import com.wolfcode.customer.domain.Customer;
import com.wolfcode.customer.service.ICustomerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wolfcode.common.annotation.Log;
import com.wolfcode.common.core.controller.BaseController;
import com.wolfcode.common.core.domain.AjaxResult;
import com.wolfcode.common.enums.BusinessType;
import com.wolfcode.contract.domain.Contract;
import com.wolfcode.contract.service.IContractService;
import com.wolfcode.common.utils.poi.ExcelUtil;
import com.wolfcode.common.core.page.TableDataInfo;

/**
 * 合同信息Controller
 * 
 * @author wolfcode
 * @date 2022-12-10
 */
@RestController
@RequestMapping("/contract/contract")
public class ContractController extends BaseController
{
    @Autowired
    private IContractService contractService;
    @Autowired
    private ICustomerService customerService;

    /**
     * 查询合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContractVo contract)
    {
        startPage();
        List<Contract> contracts = contractService.selectContractList(contract); // 合同
        return getDataTable(contracts);
    }

    /**
     * 导出合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:export')")
    @Log(title = "合同信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contract contract)
    {
        List<Contract> list = contractService.selectContractList(contract);
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        util.exportExcel(response, list, "合同信息数据");
    }


    /**
     * 新增合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:add')")
    @Log(title = "合同信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(ContractInfo contractInfo)
    {
        //contractService.insertContract(contractInfo);
//        System.out.println(contractInfo);
//        return toAjax(contractService.insertContract(contract));

        return toAjax(1);
    }

    /**
     * 修改合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:edit')")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ContractInfo contractInfo)
    {
        return toAjax(contractService.updateContract(contractInfo));
    }

    /**
     * 删除合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:remove')")
    @Log(title = "合同信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(contractService.deleteContractById(id));
    }

    /**
     * 审核通过合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:auditpass')")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping("/auditpass")
    public AjaxResult auditpass(@RequestBody ContractInfo contractInfo)
    {
        return toAjax(contractService.auditpass(contractInfo));
    }
    /**
     * 审核不通过合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:auditnopass')")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping("/auditnopass")
    public AjaxResult auditnopass(@RequestBody ContractInfo contractInfo)
    {
        return toAjax(contractService.auditnopass(contractInfo));
    }
    /**
     * 确认盖章合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:affix')")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping("/affix")
    public AjaxResult affix(@RequestBody ContractInfo contractInfo)
    {
        return toAjax(contractService.affix(contractInfo));
    }

    /**
     * 获取客户联系人详细信息
     */
    @PreAuthorize("@ss.hasPermi('contract:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Contract contract = contractService.selectContractById(id);
        return AjaxResult.success(contract);
    }
}
